/**
 * Created by gitbong on 14-7-10.
 * base on jq1.6.2
 */
var gb = gb = gb || {};
(function () {
    $(function () {
        gb.console._init();
    })
    gb.console = {
        _inited: false,
        _$view: null,
        _lineNum: 1,
        //
        _touchNum: 0,
        _loopId: -1,
        _isStart: false,
        _isShow: false,
        _okNum: 0,
        //
        enabled: true,
        _isStop: false,
        _init: function () {
            var self = this;
            if (this._inited)return;
            this._inited = true;

            $('body').append('\
                <div id="gbdebug" style="display:none;overflow: scroll">\
                    <button class="gbclose-btn" style="opacity: .5;font-weight:bold;width:60px;height:40px;position:fixed;bottom:0;right:0;border:#666;background:#222;color:#fff">Close</button>\
                    <button class="gbstop-btn" style="opacity: .5;font-weight:bold;width:60px;height:40px;position:fixed;bottom:41px;right:0;border:none;border:#666;background:#222;color:#f00">Stop</button>\
                    <button class="gbclean-btn" style="opacity: .5;font-weight:bold;width:60px;height:40px;position:fixed;bottom:82px;right:0;border:none;border:#666;background:#222;color:#fff">Clean</button>\
                </div>\
            ');
            this._$view = $('#gbdebug');

            var w = document.documentElement.clientWidth;
            var h = document.documentElement.clientHeight;
            this._$view.css({'z-index': '99999999999999527', "letter-spacing": "2px", "overflow": "auto", "font-family": "aril", left: 0, top: 0, position: 'absolute', 'line-height': '16px', 'font-size': '12px', color: '#fff', 'background-color': '#000', opacity: '.8'});
            self._$view.css({width: "100%", height: "100%"});

            $('.gbclose-btn').bind('touchstart', function () {
                self._hide();
            })
            $('.gbclean-btn').bind('touchstart', function () {
                $('#gbdebug div').remove();
            })
            $('.gbstop-btn').bind('touchstart', function () {
                self._isStop = !self._isStop;
                if (self._isStop) {
                    $(this).html('Start').css('color', "#D58B48");
                } else {
                    $(this).html('Stop').css('color', "#f00");
                }
            })

            window.addEventListener('touchstart', function (e) {
                if (self._isShow)return;
                self._touchNum++;
                clearTimeout(self._loopId);

                var x = e.touches[0].clientX;
                if (self._touchNum == 1 || self._touchNum == 3) {
                    if (x < w / 3) {
//                        console.log('L');
                        self._okNum++;
                    } else {
                        self._touchNum = 0;
                        self._okNum = 0;
                    }
                }

                if (self._touchNum == 2 || self._touchNum == 4) {
                    if (x > w / 3 * 2) {
//                        console.log('R');
                        self._okNum++;
                    } else {
                        self._touchNum = 0;
                        self._okNum = 0;
                    }
                }

                if (self._okNum == 4) {
                    self._touchNum = self._okNum = 0;
//                    console.log('run');
                    self._show();
                }
                self._loopId = setTimeout(function () {
                    self._touchNum = self._okNum = 0;
                }, 2000);
            })
        },
        log: function (msg_) {
            if (this._isStop)return;
            var str = "<a style='color:#11b1d6'>" + this._lineNum + ':</a>';

            var m = '';
            for (var i in arguments) {
                m += this._onType(arguments[i]) + ',';
            }

            m = "<a style='padding-left:5px;'>" + m.substr(0, m.length - 1) + "</div>";

            this._$view.append("<div>" + str + m + "</div>");

            this._lineNum++;
        },
        _onType: function (v_) {
            var str = '';
            if (v_ == null) {
                str += this._onNull(v_);
            } else {
                switch (typeof (v_)) {
                    case 'string':
                    {
                        str += this._onString(v_);
                        break;
                    }
                    case 'number':
                    {
                        str += this._onNumber(v_);
                        break;
                    }
                    case 'object':
                    {
                        if (v_.length) {
                            str += this._onArr(v_);
                        } else {
                            str += this._onObj(v_);
                        }
                        break;
                    }
                    case 'boolean':
                    {
                        str += this._onBoolean(v_);
                        break;
                    }
                }
            }
            return str;
        },
        _onNull: function (v_) {
            return 'null';
        },
        _onString: function (v_) {
            return '"' + v_ + '"';
        },
        _onNumber: function (v_) {
            return v_;
        },
        _onObj: function (v_) {
            var str = '{';
            for (var i in v_) {
                if (v_[i] == null) {
                    str += i + ':' + this._onNull(v_[i]);
                } else {
                    switch (typeof(v_[i])) {
                        case 'string':
                        {
                            str += i + ':' + this._onString(v_[i]);
                            break;
                        }
                        case 'number':
                        {
                            str += i + ':' + this._onNumber(v_[i]);
                            break;
                        }
                        case 'object':
                        {
                            if (v_[i].length) {
                                str += i + ":" + this._onArr(v_[i]);
                            } else {
                                str += i + ":" + this._onObj(v_[i]);
                            }
                            break;
                        }
                        case 'boolean':
                        {
                            str += i + ":" + this._onBoolean(v_[i]);
                            break;
                        }
                    }
                }
                str += ',';
            }
            str = str.substr(0, str.length - 1);
            str += '}';
            return str;
        },
        _onArr: function (v_) {
            var str = '[';
            for (var i = 0; i < v_.length; i++) {
                if (v_[i] == null) {
                    str += this._onNull(v_[i]);
                } else {
                    switch (typeof(v_[i])) {
                        case 'string':
                        {
                            str += this._onString(v_[i]);
                            break;
                        }
                        case 'number':
                        {
                            str += this._onNumber(v_[i]);
                            break;
                        }
                        case 'object':
                        {
                            if (v_[i].length) {
                                str += this._onArr(v_[i]);
                            } else {
                                str += this._onObj(v_[i]);
                            }
                            break;
                        }
                        case 'boolean':
                        {
                            str += this._onBoolean(v_[i]);
                            break;
                        }
                    }
                }
                str += ',';
            }
            str = str.substr(0, str.length - 1);
            str += ']';
            return str;
        },
        _onBoolean: function (v_) {
            return v_ + '';
        },
        _show: function () {
            this._$view.show();
            this._isShow = true;
        },
        _hide: function () {
            this._$view.hide();
            this._isShow = false;
        }
    }
})();